package com.alorm.core.sqlite;

import java.util.List;

import com.alorm.core.exceptions.AlormException;
import com.alorm.core.sqlite.sqlgen.DataBaseSchemaGenerator;
import com.alorm.utils.Configuration;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHandler extends SQLiteOpenHelper {

	private DataBaseSchemaGenerator schemaGenerator;

	private Configuration conf;

	public DataBaseHandler(Context context, Configuration conf,
			CursorFactory factory, int version) {
		super(context, conf.getDataBaseName(), factory, version);
		this.conf = conf;
		schemaGenerator = new DataBaseSchemaGenerator(context);
	}

	public void onCreate(SQLiteDatabase db) {

		try {

			if ("create".equals(conf.getCreationMode())) {

				List<String> list = schemaGenerator.CreatTables();

				for (String it : list) {
					db.execSQL(it);

				}

			}

			else if ("noaction".equals(conf.getCreationMode())) {
				// on ne fait rien
			}

		} catch (Exception e) {
			throw new AlormException(
					"erreur de parsing du fichier xml de configuration", e);
		}

	}

	public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {

	}

}
